﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using PhotoGallery.DAL;
using PhotoGallery.Model;
namespace PhotoGallery.DLL
{
    public class CommentDataAccess
    {
        commentDataContext cdc = new commentDataContext();
        /*插入新纪录*/
        public void insert(String pid, String uid, String sid, String content, String share, String state) 
        {
            comment comment = new comment();
            comment.pid = int.Parse(pid);
            comment.uid = int.Parse(uid);
            comment.sid = int.Parse(sid);
            comment.content = content;
            comment.share = char.Parse(share);
            comment.time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            comment.state = char.Parse(state);
            cdc.comments.InsertOnSubmit(comment);
            cdc.SubmitChanges();
        }
        /*删除评论*/
        public void delete(int id) 
        {
            var result = from m in cdc.comments
                         where m.cid == id
                         select m;
            foreach(comment m in result)
                cdc.comments.DeleteOnSubmit(m);
            cdc.SubmitChanges();
        }
        /*按照id查找评论*/
        public Comment searchCommentById(string id) 
        {
            int cid = int.Parse(id);
            var query = cdc.comments.Where(m => m.cid == cid).Select(m => new Comment(m.cid.ToString(), m.pid.ToString(), m.uid.ToString(), m.sid.ToString(), m.content, m.share.ToString(), m.time, m.state.ToString()));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*按照图片id查找评论*/
        public List<Comment> searchCommentByPicId(string id)
        {
            List<Comment> list=new List<Comment>();
            int pid = int.Parse(id);
            var query = cdc.comments.Where(m => m.pid == pid && m.state == '0').Select(m => new Comment(m.cid.ToString(), m.pid.ToString(), m.uid.ToString(), m.sid.ToString(), m.content, m.share.ToString(), m.time, m.state.ToString()));
            foreach (var m in query)
                if (m != null)
                   list.Add(m);
               
            return list;
        }
        /*某个用户收到的评论*/
        public List<Comment> searchCommentBySid(string sid)
        {
            List<Comment> list = new List<Comment>();
            var query = cdc.comments.Where(m => m.sid == int.Parse(sid)).Select(m => new Comment(m.cid.ToString(), m.pid.ToString(), m.uid.ToString(), m.sid.ToString(), m.content, m.share.ToString(), m.time, m.state.ToString()));
            foreach (var m in query)
                if (m != null)
                    list.Add(m);

            return list;
        }
        /*按照图片和用户id查找评论*/
        public List<Comment> searchCommentByPidAndUid(string pid,string uid)
        {
            List<Comment> list = new List<Comment>();
            var query = 
                cdc.comments.Where(m => m.pid == int.Parse(pid) && m.uid == int.Parse(uid))
                .Select(m => new Comment(m.cid.ToString(), m.pid.ToString(), m.uid.ToString(), m.sid.ToString(), m.content, m.share.ToString(), m.time, m.state.ToString()));
            foreach (var m in query)
                if (m != null)
                    list.Add(m);
            return list;
        }
        /*查找某人的收藏（state为1的）*/
        public List<Picture> searchFavorPic(string uid) 
        {
            PictureDataAccess pda=new PictureDataAccess();
            List<Picture> list = new List<Picture>();
            var query = from m in cdc.comments
                        where m.uid == int.Parse(uid) && m.state == '1'
                        select m;
            foreach(comment m in query)
            {
                if (m != null) 
                {
                    Picture p=pda.searchpictureById(m.pid.ToString());
                    if (p != null)
                        list.Add(p);
                }
            }
            return list;
        }
        /*取消收藏(实际上是删除这个评论记录)*/
        public void unFavor(string pid, string uid) 
        {
            var query = from m in cdc.comments
                        where m.uid == int.Parse(uid) && m.state == '1' &&m.pid==int.Parse(pid)
                        select m;
            foreach (comment m in query) 
            {
                cdc.comments.DeleteOnSubmit(m);
            }
            cdc.SubmitChanges();
        }
        /*修改评论*/
        public void modifyComment(string cid,Comment comment)
        {
            var result = from m in cdc.comments
                         where m.cid == int.Parse(cid)
                         select m;
            foreach (comment m in result)
            {
                m.content = comment.Content;
                m.state = char.Parse(comment.State);
            }
            cdc.SubmitChanges();
        }
    }
}
